package cl.orangepeople.whyem.portal.backend.api.impl;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.Locale;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cl.orangepeople.whyem.portal.backend.api.DebtsService;
import cl.orangepeople.whyem.portal.backend.dto.debts.DebtDTO;
import cl.orangepeople.whyem.portal.backend.dto.debts.DebtDataBean;
import cl.orangepeople.whyem.portal.backend.dto.debts.DebtDataDetails;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class DebtsServiceImpl implements DebtsService {

    private static Logger logger;
    private static String[] clientsID;
    private static String[] paymentNumbers;

    static {
	logger = LoggerFactory.getLogger(DebtsServiceImpl.class);
	clientsID = new String[] { "15956647", "158011638", "111111111", "15801163", "11111111"};
	paymentNumbers = new String[] { "12345678" };
    }

    public DebtDataBean getDebtDetails(DebtDTO paymentData) {
        String clientID = paymentData.getClientID();
        String paymentNumber = paymentData.getPaymentID();

        Object[] objectToBeLogged = new Object[]{clientID, paymentNumber};
        logger.debug("Client id: {}, payment number: {}", objectToBeLogged);

        DebtDataBean bean = new DebtDataBean();
        DebtDataDetails newPaymentData = new DebtDataDetails();

        boolean existsClientID = ArrayUtils.contains(clientsID, clientID);
        boolean existsPayment = ArrayUtils.contains(paymentNumbers, paymentNumber);
        boolean existsPaymebtData = existsClientID && existsPayment;
        if (existsPaymebtData) {
            bean.setReturnCode(0);
            newPaymentData.setClientID(new BigDecimal("15956647"));
            newPaymentData.setClientName("Paolo Mezzano");
            newPaymentData.setDescription("Cr\u00e9dito de consumo");

            Locale locale = new Locale("es", "CL");
            NumberFormat numberFormatter = NumberFormat.getCurrencyInstance(locale);
            String trxAmmountFormatted = numberFormatter.format(new BigDecimal("295990"));
            trxAmmountFormatted = StringUtils.remove(trxAmmountFormatted, "Ch");
            newPaymentData.setAmount(trxAmmountFormatted);
        } else {
            bean.setReturnCode(-1);
        }

        bean.setPaymentDetail(newPaymentData);

        return bean;
    }

    public List<DebtDataBean> getDebtsByClientID(String clientID) {
        List<DebtDataBean> debts;
        boolean existClientID = ArrayUtils.contains(clientsID, clientID);
        if (existClientID) {
            debts = new ArrayList<DebtDataBean>() {
                {
                    add(new DebtDataBean() {
                        {
                            setReturnCode(0);
                            setPaymentDetail(new DebtDataDetails() {
                                {
                                    setClientID(new BigDecimal("123"));
                                    setClientName("Banco Chile");
                                    setProductName("Crédito hipotecario");
                                    setPaymentNumber(new BigDecimal("1234567890"));
                                    setDescription("Cuota N° 02");
                                    setAmount("200000");
                                }
                            });
                        }
                    });
                    add(new DebtDataBean() {
                        {
                            setReturnCode(0);
                            setPaymentDetail(new DebtDataDetails() {
                                {
                                    setClientID(new BigDecimal("123"));
                                    setClientName("Banco Chile");
                                    setProductName("Crédito de consumo");
                                    setPaymentNumber(new BigDecimal("1234567891"));
                                    setDescription("Cuota N° 01");
                                    setAmount("200000");
                                }
                            });
                        }
                    });
                    add(new DebtDataBean() {
                        {
                            setReturnCode(0);
                            setPaymentDetail(new DebtDataDetails() {
                                {
                                    setClientID(new BigDecimal("223"));
                                    setClientName("Banco Santander");
                                    setProductName("Tarjeta de Crédito");
                                    setPaymentNumber(new BigDecimal("9876543210"));
                                    setDescription("Facturación Junio");
                                    setAmount("150000");
                                }
                            });
                        }
                    });
                }
            };
        } else {
            debts = Collections.emptyList();
        }
        return debts;
    }
}
